﻿using AutoMapper;
using KayakMes.Common;
using KayakMes.Common.Dtos.ProductControlDTO;
using KayakMes.Common.Dtos.ProductControlDTO.Input;
using KayakMes.Common.Dtos.ProductControlDTO.Output;
using KayakMes.Common.Result;
using KayakMes.ProductControl.Domain.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.ProductControl.Application.Services
{
    /// <summary>
    /// 产品管理服务层
    /// </summary>
    public class ProductService : IProductService
    {
        private readonly BaseReposiotry<ProductModel> productRepo;
        private readonly BaseReposiotry<ReelNumberModel> reelNumberRepo;
        private readonly BaseReposiotry<ColourModel> colourRepo;
        private readonly BaseReposiotry<TypeModel> typeRepo;
        private readonly BaseReposiotry<SpecificationModel> specificationRepo;
        private readonly IMapper mapper;

        /// <summary>
        /// 通过构造函数注入所需要的资源
        /// </summary>
        /// <param name="productRepo">产品仓储</param>
        /// <param name="reelNumberRepo">盘号仓储</param>
        public ProductService(BaseReposiotry<ProductModel> productRepo,BaseReposiotry<ReelNumberModel> reelNumberRepo,BaseReposiotry<ColourModel> colourRepo,BaseReposiotry<TypeModel> typeRepo,BaseReposiotry<SpecificationModel> specificationRepo,IMapper mapper)
        {
            this.productRepo = productRepo;
            this.reelNumberRepo = reelNumberRepo;
            this.colourRepo = colourRepo;
            this.typeRepo = typeRepo;
            this.specificationRepo = specificationRepo;
            this.mapper = mapper;
        }
        #region 产品管理—产品操作
        /// <summary>
        /// 添加产品信息
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddProduct(ProductModel product)
        {
            product.Id=YitIdHelper.NextId();
            var result=productRepo.Insert(product);
            if(result)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "添加成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "添加失败", Data = 0 };
            }
        }
        /// <summary>
        /// 获取所有产品信息以及分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<ProductModel> GetAllProduct(ProductDto dto)
        {
            var result = productRepo.GetAll();
            if (!string.IsNullOrEmpty(dto.MaterialCode))
            {
                result = result.Where(x => x.MaterialCode == dto.MaterialCode);
            }
            if (!string.IsNullOrEmpty(dto.MaterialName))
            {
                result = result.Where(x => x.MaterialName == dto.MaterialName);
            }
            ApiPaging<ProductModel> apiPaging = new ApiPaging<ProductModel>
            {
                Total = result.Count(),
                PageIndex = dto.Index,
                PageSize = dto.Size,
                Items = result.Skip((dto.Index - 1) * dto.Size).Take(dto.Size).ToList()
            };
            return apiPaging;
        }
        /// <summary>
        ///通过Id获取产品信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<ProductModel> GetProduct(long Id)
        {
            var result=productRepo.GetModel(Id);
            if(result==null)
            {
                return new ApiResult<ProductModel> { Code = ApiEnums.NotFound, Message = "产品未找到", Data = null };
            }
            if(result.IsDeleted==true)
            {
                return new ApiResult<ProductModel> { Code = ApiEnums.NotFound, Message = "产品已被删除", Data = null };
            }
            return new ApiResult<ProductModel> { Code = ApiEnums.Success, Message = "产品查询成功", Data = result };
        }
        /// <summary>
        ///更新产品信息
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> UpdateProduct(ProductModel product)
        {
            var result = productRepo.Update(product); 
            if(result > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "更新成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "更新失败", Data = 0 };
            }
        }
        /// <summary>
        /// 逻辑删除产品信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> LoginDel(long Id)
        {
            var result = productRepo.GetModel(Id);
            if (result == null || result.IsDeleted == true)
            {
                return new ApiResult<int> { Code = ApiEnums.NotFound, Message = "产品未找到或已被删除", Data = 0 };
            }
            result.IsDeleted = true;
            var updateResult = productRepo.Update(result);
            if (updateResult > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "产品删除成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "产品删除失败", Data = 0 };
            }
        }
        #endregion
        #region 产品管理—盘号操作
        /// <summary>
        /// 添加盘号信息
        /// </summary>
        /// <param name="reelNumber">盘号实体</param>
        /// <returns>返回操作结果以及受影响行数</returns>
        public ApiResult<int> AddReelNumber(ReelNumberModel reelNumber)
        {
            reelNumber.Id = YitIdHelper.NextId();
            var result = reelNumberRepo.Add(reelNumber);
            if (result > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "添加成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "添加失败", Data = 0 };
            }
        }
        /// <summary>
        /// 获取所有盘号信息以及分页查询
        /// </summary>
        /// <param name="dto">盘号Dto类</param>
        /// <returns>返回操作结果以及受影响行数</returns>
        public ApiPaging<ReelNumberModel> GetAllReelNumber(ReelNumberDto dto)
        {
            var result = reelNumberRepo.GetAll();
            ApiPaging<ReelNumberModel> apiPaging = new ApiPaging<ReelNumberModel>
            {
                Total = result.Count(),
                PageIndex = dto.Index,
                PageSize = dto.Size,
                Items = result.Skip((dto.Index - 1) * dto.Size).Take(dto.Size).ToList()
            };
            return apiPaging;
        }
        /// <summary>
        /// 通过Id获取盘号信息
        /// </summary>
        /// <param name="Id">盘号Id</param>
        /// <returns>返回操作结果以及受影响行数</returns>
        public ApiResult<ReelNumberModel> GetReelNumber(long Id)
        {
            var result=reelNumberRepo.GetModel(Id);
            if(result==null)
            {
                return new ApiResult<ReelNumberModel> { Code = ApiEnums.NotFound, Message = "盘号未找到", Data = null };
            }
            if(result.IsDeleted==true)
            {
                return new ApiResult<ReelNumberModel> { Code = ApiEnums.NotFound, Message = "盘号已被删除", Data = null };
            }
            return new ApiResult<ReelNumberModel> { Code = ApiEnums.Success, Message = "盘号查询成功", Data = result };
        }
        /// <summary>
        /// 更新盘号信息
        /// </summary>
        /// <param name="reelNumber">盘号实体</param>
        /// <returns>返回操作结果以及受影响行数</returns>
        public ApiResult<int> UpdateReelNumber(ReelNumberModel reelNumber)
        {
            var result = reelNumberRepo.Update(reelNumber);
            if(result > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "更新成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "更新失败", Data = 0 };
            }
        }
        /// <summary>
        /// 删除盘号信息
        /// </summary>
        /// <param name="Id">盘号ID</param>
        /// <returns>返回操作结果以及受影响行数</returns>
        public ApiResult<int> DelReelNumber(long Id)
        {
            var result=reelNumberRepo.GetModel(Id);
            if(result==null || result.IsDeleted==true)
            {
                return new ApiResult<int> { Code = ApiEnums.NotFound, Message = "盘号未找到或已被删除", Data = 0 };
            }
            result.IsDeleted = true;
            var updateResult = reelNumberRepo.Update(result);
            if (updateResult > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "盘号删除成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "盘号删除失败", Data = 0 };
            }
        }
        #endregion
        #region 产品管理—颜色操作
        /// <summary>
        /// 添加颜色信息
        /// </summary>
        /// <param name="colour">颜色模型</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<int> AddColour(ColourModel colour)
        {
            colour.Id=YitIdHelper.NextId();
            var result=colourRepo.Add(colour);
            if(result>0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "颜色添加成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "颜色添加失败", Data = 0 };
            }
        }
        /// <summary>
        /// 获取所有颜色信息
        /// </summary>
        public ApiPaging<ColourModel> GetAllColour(ColourDto dto)
        {
            var result=colourRepo.GetAll();
            if (!string.IsNullOrEmpty(dto.ColourName))
            {
                result = result.Where(x => x.ColourName == dto.ColourName);
            }
            ApiPaging<ColourModel> apiPaging = new ApiPaging<ColourModel>
            {
                Total = result.Count(),
                PageIndex = dto.Index,
                PageSize = dto.Size,
                Items = result.Skip((dto.Index - 1) * dto.Size).Take(dto.Size).ToList()
            };
            return apiPaging;
        }
        /// <summary>
        /// 通过Id获取颜色信息
        /// </summary>
        /// <param name="Id">颜色ID</param>
        /// <returns></returns>
        public ApiResult<ColourModel> GetColour(long Id)
        {
            var result=colourRepo.GetModel(Id);
            if(result==null)
            {
                return new ApiResult<ColourModel>
                {
                    Code = ApiEnums.NotFound,
                    Message = "未找到该颜色信息",
                    Data = null
                };
            }
            if(result.IsDeleted==true)
            {
                return new ApiResult<ColourModel>
                {
                    Code = ApiEnums.Fail,
                    Message = "该颜色信息已被删除",
                    Data = null
                };
            }
            return new ApiResult<ColourModel>
            {
                Code = ApiEnums.Success,
                Message = "颜色信息查询成功",
                Data = result
            };
        }
        /// <summary>
        /// 更新颜色信息
        /// </summary>
        /// <param name="colour">颜色模型</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<int> UpdateColour(ColourModel colour)
        {
            var result=colourRepo.Update(colour);
            if (result > 0) 
            { 
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "颜色信息更新成功", Data = 1 };
            }
            else
            {
               return new ApiResult<int> { Code = ApiEnums.Fail, Message = "颜色信息更新失败", Data = 0 };
            }
        }
        /// <summary>
        /// 删除颜色信息
        /// </summary>
        /// <param name="Id">颜色实体Id</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<int> DelColour(long Id)
        {
            var result = colourRepo.GetModel(Id);
            if (result == null || result.IsDeleted == true)
            {
                return new ApiResult<int> { Code = ApiEnums.NotFound, Message = "颜色信息未找到或已被删除", Data = 0 };
            }
            result.IsDeleted = true;
            var updateResult = colourRepo.Update(result);
            if (updateResult > 0) 
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "颜色信息删除成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "颜色信息删除失败", Data = 0 };
            }
        }
        #endregion
        #region 产品管理—型号操作
        /// <summary>
        /// 添加型号信息
        /// </summary>
        /// <param name="product">产品实体</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<int> AddType(TypeModel type)
        {
            type.Id=YitIdHelper.NextId();
            type.UpdateUserId = "admin";
            type.CreateUserId = "admin";
            var result = typeRepo.Add(type);
            if (result > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "型号信息添加成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "型号信息添加失败", Data = 0 };
            }
        }
        /// <summary>
        /// 获取所有型号信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ApiPaging<TypeModel> GetAllType(TypeDto dto)
        {
            var result=typeRepo.GetAll().Where(x=>x.IsDeleted==false);
            if (!string.IsNullOrEmpty(dto.TypeCode))
            {
                result = result.Where(x => x.TypeCode.Contains(dto.TypeCode));
            }
            ApiPaging<TypeModel> apiPaging = new ApiPaging<TypeModel>
            {
                Total = result.Count(),
                PageIndex = dto.Index,
                PageSize = dto.Size,
                Items = result.Skip((dto.Index - 1) * dto.Size).Take(dto.Size).ToList()
            };
            return apiPaging;
        }
        /// <summary>
        /// 获取指定型号信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ApiResult<TypeModel> GetType(long Id)
        {
            var result=typeRepo.GetModel(Id);
            if (result == null)
            {
                return new ApiResult<TypeModel>
                {
                    Code = ApiEnums.Fail,
                    Message = "未找到该型号信息",
                    Data = null
                };
            }
            if(result.IsDeleted == true)
            {
                return new ApiResult<TypeModel>
                {
                    Code = ApiEnums.Fail,
                    Message = "该型号信息已被删除",
                    Data = null
                };
            }
            return new ApiResult<TypeModel>
            {
                Code = ApiEnums.Success,
                Message = "型号信息查询成功",
                Data = result
            };
        }
        /// <summary>
        /// 删除指定型号信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ApiResult<int> DelType(long Id)
        {
            var result = typeRepo.GetModel(Id);
            if(result == null || result.IsDeleted == true)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.NotFound,
                    Message = "型号信息未找到或已被删除",
                    Data = 0
                };
            }
            result.IsDeleted = true;
            var updateResult = typeRepo.Update(result);
            if (updateResult > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "型号信息删除成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "型号信息删除失败", Data = 0 };
            }
        }
        /// <summary>
        /// 修改型号信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateType(TypeModel type)
        {
            var result = typeRepo.Update(type);
            if (result > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "修改型号信息成功", Data = result };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "修改型号信息失败", Data = 0 };
            }
        }
        #endregion
        #region 产品管理—规格操作
        /// <summary>
        /// 添加规格信息
        /// </summary>
        /// <param name="input">规格实体输入参数</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<int> AddSpecification(AddSpecificationInput input)
        {
            var entity = mapper.Map<SpecificationModel>(input);
            entity.Id=YitIdHelper.NextId();
            var result = specificationRepo.Add(entity); 
            if(result>0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "添加规格信息成功",
                    Data = result
                };
            }
            else
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "添加规格信息失败",
                    Data = result
                };
            }
        }
        /// <summary>
        /// 获取规格所有信息
        /// </summary>
        /// <param name="input">获取规格信息输入参数</param>
        /// <returns>返回受影响行数</returns>
        public ApiPaging<SpecificationOutput> GetAllSpecification(GetSpecificationInput input)
        {
            var entity = specificationRepo.GetAll().Where(x => x.IsDeleted == false);
            if(input.TypeId != null)
            {
                entity = entity.Where(x => x.TypeId == input.TypeId);
            }
            int count = entity.Count();
            var list = entity.OrderBy(x => x.Id).Skip((input.Index - 1) * input.Size).Take(input.Size).ToList();
            var model=mapper.Map<List<SpecificationOutput>>(list);
            return new ApiPaging<SpecificationOutput>
            {
                Total = count,
                PageIndex = input.Index,
                PageSize = input.Size,
                Items = model
            };
            
        }
        /// <summary>
        /// 根据指定Id获取规格信息
        /// </summary>
        /// <param name="Id">指定规格Id</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<SpecificationOutput> GetSpecificationCount(long Id)
        {
            var model = specificationRepo.GetModel(Id);
            if (model == null)
            {
                return new ApiResult<SpecificationOutput> { Code = ApiEnums.NotFound, Message = "规格未找到或已被删除", Data = null };
            }
            return new ApiResult<SpecificationOutput>
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = mapper.Map<SpecificationOutput>(model)
            };
        }
        /// <summary>
        /// 更新规格信息
        /// </summary>
        /// <param name="specification">规格实体参数</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<int> UpdateSpecification(SpecificationOutput specification)
        {
            var model = specificationRepo.GetModel(specification.Id);
            if (model != null && model.IsDeleted == false)
            {
                var entity = mapper.Map<SpecificationModel>(specification);
                var result = specificationRepo.Update(entity);
                if (result > 0)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Message = "更新成功",
                        Data = result
                    };
                }
                else
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Message = "更新失败",
                        Data = result
                    };
                }
            }
            else
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Message = "找不到要修改的信息！",
                    Data = -1
                };
            }
        }
        /// <summary>
        /// 删除规格信息
        /// </summary>
        /// <param name="Id">规格信息Id</param>
        /// <returns>返回受影响行数</returns>
        public ApiResult<int> DelSpecification(long Id)
        {
            var result = specificationRepo.GetModel(Id);
            if(result != null)
            {
                var updateResult = specificationRepo.Delete(result);
                if(updateResult > 0)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Message = "删除成功！",
                        Data = 1
                    };
                }
                else
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Message = "删除失败！",
                        Data = 0
                    };
                }
            }
            else
            {
                return new ApiResult<int>
                {
                    Code = ApiEnums.Fail,
                    Message = "找不到删除信息！",
                    Data = 0
                };
            }
        }
        #endregion
    }
}
